package com.hz.ruoyiequipment.equipment.controller;

import com.hz.ruoyiequipment.data.domain.AgrEquipment;
import com.hz.ruoyiequipment.equipment.domain.AgrEquipmentFertigation;
import com.hz.ruoyiequipment.equipment.domain.AgrEquipmentSf;
import com.hz.ruoyiequipment.equipment.domain.AgrFertigationData;
import com.hz.ruoyiequipment.equipment.domain.vo.WafEquipment;
import com.hz.ruoyiequipment.equipment.service.WafEquipmentService;
import com.hz.ruoyiequipment.equipment.service.impl.SfPlotsdRedisService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * @author pan
 * @date 2025/3/8 17:27
 * @description: 水肥一体机控制层
 */

@RestController
@RequestMapping("/waf")
@CrossOrigin
public class WafEquipmentController extends BaseController {

    @Autowired
    private WafEquipmentService wafEquipmentService;
    @Autowired
    private ScheduledExecutorService scheduledExecutorService;
    @Autowired
    private SfPlotsdRedisService sfPlotsdRedisService;

    @GetMapping("/getEqPlotsRedis")
    public AjaxResult getEqPlotsRedis(Long equipmentId){
        System.out.println("++++++++++++++++++++"+equipmentId);
        return AjaxResult.success(sfPlotsdRedisService.getThresholdRules(equipmentId));
    }
    @RequiresPermissions("equipment:waf:list")
    @GetMapping("/list")
    public AjaxResult getWafEquipment(Integer equipmentStatus, String equipmentCode, String plotsName){
        return wafEquipmentService.getWafEquipment(equipmentStatus, equipmentCode, plotsName);
    }

    @RequiresPermissions("equipment:waf:detail")
    @GetMapping("/detail")
    public AjaxResult getWafDetail(Long equipmentId){
        return wafEquipmentService.getWafDetail(equipmentId);
    }

    @RequiresPermissions("equipment:waf:insert")
    @PostMapping("/insert")
    public AjaxResult insertSf(@RequestBody WafEquipment wafEquipment){
        try {
            AgrEquipment agrEquipment = new AgrEquipment();
            agrEquipment.setEquipmentCode(wafEquipment.getEquipmentCode());
            agrEquipment.setEquipmentName(wafEquipment.getEquipmentName());
            agrEquipment.setEquipmentBrand(wafEquipment.getEquipmentBrand());
            agrEquipment.setEquipmentType(wafEquipment.getEquipmentType());
            agrEquipment.setEquipmentImg(wafEquipment.getEquipmentImg());

            AgrEquipmentSf agrEquipmentSf = getAgrEquipmentSf(wafEquipment);
            wafEquipmentService.insertWafEquipment(agrEquipment, agrEquipmentSf);
            return AjaxResult.success("新增设备成功");
        } catch (Exception e) {
            return AjaxResult.error("新增设备失败");
        }
    }

    public static AgrEquipmentSf getAgrEquipmentSf(WafEquipment wafEquipment) {
        AgrEquipmentSf agrEquipmentSf = new AgrEquipmentSf();
        agrEquipmentSf.setSfChannel(wafEquipment.getSfChannel());
        agrEquipmentSf.setSfPump(wafEquipment.getSfPump());
        agrEquipmentSf.setSfInput(wafEquipment.getSfInput());
        agrEquipmentSf.setSfFlowRate(wafEquipment.getSfFlowRate());
        agrEquipmentSf.setSfControl(wafEquipment.getSfControl());
        agrEquipmentSf.setSfMixing(wafEquipment.getSfMixing());
        agrEquipmentSf.setSfControlMode(wafEquipment.getSfControlMode());
        agrEquipmentSf.setSfInterface(wafEquipment.getSfInterface());
        agrEquipmentSf.setSfProtection(wafEquipment.getSfProtection());
        return agrEquipmentSf;
    }

    @RequiresPermissions("equipment:waf:update")
    @PostMapping("/update")
    public AjaxResult updateWafEquipment(@RequestBody WafEquipment wafEquipment){
        List<Integer> plotsIdList = new ArrayList<>();
        if (wafEquipment.getPlotsId() != null && !wafEquipment.getPlotsId().isEmpty()){
            String plotsIdStr = wafEquipment.getPlotsId();
            String[] plotsIdArr = plotsIdStr.split(",");
            for (String plotsId : plotsIdArr){
                plotsIdList.add(Integer.parseInt(plotsId));
            }
        }
        return AjaxResult.success(wafEquipmentService.updateWafEquipment(wafEquipment, plotsIdList));
    }

    @RequiresPermissions("equipment:waf:delete")
    @PostMapping("/delete")
    public AjaxResult delWafEquipmentByEquipmentId(Long equipmentId){
        return AjaxResult.success(wafEquipmentService.delWafEquipmentByEquipmentId(equipmentId));
    }

    @PostMapping("/updateAgrEquipmentFertigation")
    public AjaxResult updateAgrEquipmentFertigation(@RequestBody AgrEquipmentFertigation agrEquipmentFertigation){
        return wafEquipmentService.updateAgrEquipmentFertigation(agrEquipmentFertigation);
    }

    @GetMapping("/getWafEquipmentID")
    public AjaxResult getWafEquipmentID(Long equipmentId){
        return wafEquipmentService.getWafEquipmentID(equipmentId);
    }

    @PostMapping("/updateSfStatus")
    public AjaxResult updateSfStatus(@RequestBody AgrEquipmentFertigation agrEquipmentFertigation){
        return wafEquipmentService.updateSfStatus(agrEquipmentFertigation);
    }

    @GetMapping("/getAgrFertigationData")
    public AjaxResult getAgrFertigationData(AgrFertigationData agrFertigationData){

        return AjaxResult.success(wafEquipmentService.getAgrFertigationData(agrFertigationData.getEquipmentId()))  ;
    }

    @PostMapping("/scheduleCountdown")
    public AjaxResult scheduleCountdown(@RequestBody AgrEquipmentFertigation agrEquipmentFertigation) {
        if (agrEquipmentFertigation.getCountdownHours()  <= 0){
            return AjaxResult.error("参数错误");
        }
        long delay = (long) (agrEquipmentFertigation.getCountdownHours() * 60 * 60 * 1000);
        scheduledExecutorService.schedule(() -> {
            try {
                wafEquipmentService.updateSfStatus(agrEquipmentFertigation);

            } catch (Exception e) {
                e.printStackTrace();
            }
            // do something
        }, delay, TimeUnit.MILLISECONDS);
        return AjaxResult.success();
    }

    @GetMapping("/getAgrEquipmentFertigation")
    public AjaxResult getAgrEquipmentFertigation(Long equipmentId){
        return wafEquipmentService.getAgrEquipmentFertigation(equipmentId);
    }
    /**
     * 内部调用 获取水肥设备状态
     * @param equipmentId
     * @return
     */
    @PostMapping("/inner")
    public Integer getSfStatusByEquipmentIdInner(@RequestParam("equipmentId") Long equipmentId) {
        return wafEquipmentService.getSfStatusByEquipmentIdInner(equipmentId);
    }
}
